package com.neu.controller;

import com.neu.base.Result;
import com.neu.entity.SWF;
import com.neu.service.SWFService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/swf")
@Tag(name = "软件因素评估")
public class SWFController {

    @Autowired
    private SWFService swfService;

    @PostMapping("/add")
    @Operation(summary = "添加软件因素评估")
    public Result add(@RequestBody SWF swf){

        return Result.success(swfService.save(swf));
    }

    @PostMapping("/get")
    @Operation(summary = "添加软件因素评估")
    public Result getSWF(@RequestParam String id){
        SWF swf = swfService.getById(id);
        if(swf==null){
            swf = new SWF();
            swf.setProgress(0);
            swf.setProjectId(id);
            swf.setDelFlag("1");
            swfService.save(swf);
        }
        Map data = swf.calc();
        Map map = new HashMap<>();
        map.put("swf_data", swf);
        map.put("data", data);

        return Result.success(map);
    }

    @GetMapping("/QR")
    @Operation(summary = "添加软件因素评估")
    public Result getSWF_QR(@RequestParam String id){
        SWF swf = swfService.getById(id);
        System.out.println(swf);
        Float QR = swf.calcQR();

        return Result.success(QR);
    }

    @PostMapping("/update")
    @Operation(summary = "更新PDR")
    public Result updatePDR(@RequestBody SWF swf){
        try{
            swf.calc();
            swf.setProgress(1);
        }catch (Exception e){
            swf.setProgress(0);
        }
        Boolean sign = swfService.updateById(swf);
        if(!sign){
            sign = swfService.save(swf);
        }
        return Result.success(sign);
    }
}
